package gui.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
  步骤
    1.数据结构
    2.画上去
    3.监听
        键盘
        刷新事件
 */

public class GamePanel extends Panel implements KeyListener, ActionListener {

    //定义蛇的数据结构
    int length; //蛇的长度 864
    int[] snakeX = new int[600]; //蛇的x坐标
    int[] snakeY = new int[500]; //蛇的y坐标
    String direction = null;
    boolean isStart = false; //游戏是否开始
    Timer timer = new Timer(60,this); //定时器,100ms1次

    //食物坐标
    int foodX;
    int foodY;
    Random random = new Random();

    boolean isFail = false; //游戏是否失败

    int score;//积分

    public GamePanel(){
        init();
        //获得焦点和键盘事件
        this.setFocusable(true);
        this.addKeyListener(this);  //获得当前类，实现了监听器
        timer.start(); //定时器启动

    }

    //初始化
    public void init(){
        score = 0;
        length = 3;
        snakeX[0] = 100;snakeY[0] = 100; //头的坐标
        snakeX[1] = 75;snakeY[1] = 100; //第一节
        snakeX[2] = 50;snakeY[2] = 100; //第二节
        direction = "R";

        //食物随机分布
        foodX = 25 + 25*random.nextInt(34);
        foodY = 75 + 25*random.nextInt(24);
    }

    //绘制面板,游戏中的所有东西由画笔来画
    @Override
    public void paint(Graphics g) {
        super.paintComponents(g);
        //绘制静态面板
        this.setBackground(Color.WHITE);
        DataCenter.header.paintIcon(this,g,25,11); //广告栏
        g.fillRect(25,75,850,600); //默认的游戏界面

        //画积分
        g.setColor(Color.cyan);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度：" + length,750,35);
        g.drawString("分数：" + score,750,50);

        //画食物
        g.setColor(Color.YELLOW);
        g.fillOval(foodX,foodY,25,25);

        //把小蛇画上去
        if(this.direction.equals("R")){
            g.setColor(Color.red);
            g.fillOval(snakeX[0],snakeY[0],25,25);
        } else if (this.direction.equals("L")){
            g.setColor(Color.red);
            g.fillOval(snakeX[0],snakeY[0],25,25);
        } else if (this.direction.equals("U")){
            g.setColor(Color.red);
            g.fillOval(snakeX[0],snakeY[0],25,25);
        } else if (this.direction.equals("D")){
            g.setColor(Color.red);
            g.fillOval(snakeX[0],snakeY[0],25,25);
        }

        g.setColor(Color.green);
        for (int i = 1; i < length; i++) {
            g.fillRect(snakeX[i],snakeY[i],25,25);
        }

        //游戏状态
        if(!this.isStart){
            //设置字体
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }

        if(isFail){
            //设置字体
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败，按空格重新开始",300,300);
        }
    }

    //键盘监听事件
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode(); //获得按键码
        if(keyCode == KeyEvent.VK_SPACE){  //是空格

            if(isFail){
                isFail = false;
                init();
            }else {
                this.isStart = !this.isStart; //取反
            }
            repaint();
        }

        //小蛇移动
        if(keyCode == KeyEvent.VK_UP){
            direction = "U";
        } else if (keyCode == KeyEvent.VK_DOWN){
            direction = "D";
        } else if (keyCode == KeyEvent.VK_LEFT){
            direction = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT){
            direction = "R";
        }

    }

    //事件监听--1s/10次
    @Override
    public void actionPerformed(ActionEvent e) {
        if(this.isStart && !isFail){ //开始，小蛇动起来

            //吃食物
            if (snakeX[0] == foodX && snakeY[0] == foodY){
                length++; //长度+1
                score++; //积分+1
                //再次生成食物
                foodX = 25 + 25*random.nextInt(34);
                foodY = 75 + 25*random.nextInt(24);
            }

            //移动
            for (int i = length-1; i > 0 ; i--) {
                snakeX[i] = snakeX[i-1]; //向前移动一节
                snakeY[i] = snakeY[i-1];
            }

            if(direction.equals("R")){
                snakeX[0] += 25;
                //边界判断
                if(snakeX[0] > 850){
                    snakeX[0] = 25;
                }
            } else if (direction.equals("L")){
                snakeX[0] -= 25;
                //边界判断
                if(snakeX[0] < 25){
                    snakeX[0] = 850;
                }
            } else if (direction.equals("U")){
                snakeY[0] -= 25;
                //边界判断
                if(snakeY[0] < 75){
                    snakeY[0] = 650;
                }
            } else if (direction.equals("D")){
                snakeY[0] += 25;
                //边界判断
                if(snakeY[0] > 650){
                    snakeY[0] = 75 ;
                }
            }

            //失败判定，撞到自己
            for (int i = 1; i < length; i++) {
                if(snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            repaint();
        }
        timer.start();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    @Override
    public void keyReleased(KeyEvent e) {

    }

}


